/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function (n) {
  let arr = [],
    res = []
  bk(n,n)

  function bk(left,right){
    if(left>right) return
    if(arr.length==n*2){
      res.push([...arr].join(""))
      return
    }

    if(left>0){
      arr.push('(')
      bk(left-1,right)
      arr.pop()
    }

    if(right>0){
      arr.push(')')
      bk(left,right-1)
      arr.pop()
    }

  }
  return res
}

generateParenthesis(3)
